package com.ty.day7;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/11/30 14:19
 */
public class ThreadPoolDemo01 {
    public static void main(String[] args) {
        //创建线程池
        ExecutorService es= Executors.newFixedThreadPool(3);
        //使用线程池
        es.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Thread thread = Thread.currentThread();
                System.out.println(thread.getName()+","+thread.getId());
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("匿名内部类结束");
                return 1;
            }
        });
        es.submit(()->{
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName()+","+thread.getId());
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("lambda11");
        });
        es.submit(()->{
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName()+","+thread.getId());
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("lambda22");
        });
        es.submit(()->{
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName()+","+thread.getId());
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("lambda33");
        });
        es.shutdown();
    }
}
